// 用户的增删改查
// 引入express
const express = require("express");
// 引入数据库操作封装
const db = require("../utils/db.js");
// 创建路由模块对象
const router = express.Router();
//配置路由  http://localhost:3000/users/login
router.post("/login", (req, res) => {
    // 接收用户传递的账号和密码
    // let userName=req.query.userName;
    // let passWord=req.query.passWord;
    let {
        userName,
        passWord
    } = req.body;
    // 定义sql
    let sql = `select * from users where userName='${userName}'`;
    db.execute(sql, (rst) => {
        if (rst.length != 0) {
            // 账号存在
            // 比对密码（拿输入的密码和数据库返回的数据中的密码比对，如果一致则登录成功，不一致则提示密码错误）
            console.log(rst);
            // 取出数据库中的密码
            let pwd = rst[0].passWord;
            if (passWord == pwd) {
                res.send({
                    code: 0,
                    msg: "登录成功"
                })
            } else {
                res.send({
                    code: 1,
                    msg: "请输入正确密码！！"
                })
            }
        } else {
            // 账号不存在
            res.send({
                code: 1,
                msg: "请输入正确账号！！"
            })
        }
    })
})
// 根据用户名查询信息
router.get("/search", (req, res) => {
    let {
        userName,
        pageNum, //当前页码
        pageSize //每页显示的数据条数
    } = req.query;
    if(!pageNum || !pageSize){
        pageNum=1;
        pageSize=2;
    }
    let sql = `select * from users `;
    if (userName) {
        sql += `where userName like '%${userName}%'`;
    }
    sql+=`order by id limit ${(parseInt(pageNum)-1)*parseInt(pageSize)},${parseInt(pageSize)}`
    db.execute(sql, (rst) => {
        res.send({
            code: 0,
            data: rst
        })
    })
})

// 根据id删除一条数据
router.get("/delete", (req, res) => {
    let {
        id
    } = req.query;
    let sql = `delete from users where id=${id}`;
    db.execute(sql, (rst) => {
        if (rst.affectedRows != 0) {
            res.send({
                code: 0,
                msg: "删除成功"
            })
        } else {
            res.send({
                code: 1,
                msg: "删除失败"
            })
        }
    })
})
// 添加
router.post("/save", (req, res) => {
    let {
        userName,
        passWord
    } = req.body;
    let sql = `insert into users (userName,passWord) values ('${userName}','${passWord}')`;
    db.execute(sql, (rst) => {
        if (rst.affectedRows != 0) {
            res.send({
                code: 0,
                msg: "添加成功"
            })
        } else {
            res.send({
                code: 1,
                msg: "添加失败"
            })
        }
    })
})

//根据id查询一条数据
router.get("/findOne",(req,res)=>{
    //接收前台传递的id
   let {id}= req.query;
   let sql=`select * from users where id=${id}`;
   db.execute(sql,(rst)=>{
    //将一个对象响应给前台
    if(rst.length==0){
        res.send({
            code:1,
            msg:"无当前数据"
        })
    }else{
        res.send({
            code:0,
            msg:"查询成功",
            data:rst[0]
        })
    }
   })
})


//根据id修改一条数据
router.post("/update",(req,res)=>{
    let {id,userName,passWord}=req.body;
    let sql=`update users set userName='${userName}',password='${passWord}' where id=${id}`;
    db.execute(sql,(rst)=>{
        if(rst.affectedRows!=0){
            res.send({
                code:0,
                msg:"修改成功"
            })
        }else{
            res.send({
                code:1,
                msg:"修改失败"
            })
        }
    })
})

// 排除账号重复
router.get("/isUserName",(req,res)=>{
  let {userName}=req.query;  
  let sql=`select * from users where userName='${userName}'`;
  db.execute(sql,(rst)=>{
      if(rst.length==0){
          res.send({
              code:0,
              msg:"账号不存在"
          })
      }else{
          res.send({
              code:1,
              msg:"账号已存在"
          })
      }
  })  
})

//暴露路由模块
module.exports = router;